package crm.view.Admin;

import crm.dao.impl.CustomerSqlSeverImpl;
import crm.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdatePro implements ActionListener {
    JFrame frame = null;
    JLabel name = null;
    JLabel consumeTime = null;
    JLabel pId = null;
    JLabel createTime = null;
    JLabel PType = null;
    JLabel price = null;
    JLabel pro = null;
    JTextField consumeTimeField = null;
    JTextField pIdField = null;
    JTextField createTimeField = null;
    JTextField PTypeField = null;
    JTextField priceField = null;
    JTextField NameField = null;
    JTextField proField = null;

    JButton btn2 = null;
    JButton btn3 = null;

    JLabel label = null;

    public static void main(String[] args) {
        new UpdatePro();
    }

    public UpdatePro() {
        init();
        frame.setBounds(680, 200, 600, 700);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("修改客户产品信息");
        name = new JLabel("输入用户姓名:");
        label = new JLabel("修改客户所购产品信息");
        consumeTime = new JLabel("消费时间:");
        pId = new JLabel("产品编号:");
        createTime = new JLabel("生产日期:");
        PType = new JLabel("产品类型:");
        price = new JLabel("产品价格:");
        pro = new JLabel("产品名称:");

        NameField = new JTextField("");
        consumeTimeField = new JTextField("");
        pIdField = new JTextField("");
        createTimeField = new JTextField("");
        PTypeField = new JTextField("");
        priceField = new JTextField("");
        proField = new JTextField("");


        btn2 = new JButton("清空");
        btn3 = new JButton("修改");
        frame.add(btn2);
        frame.add(btn3);
        frame.add(label);
        Font font1 = new Font("方正黑体简体", 1, 30);
        label.setFont(font1);

        Font font = new Font("方正楷体", 2, 20);
        name.setFont(font);
        NameField.setFont(font);
        consumeTime.setFont(font);
        consumeTimeField.setFont(font);
        pId.setFont(font);
        pIdField.setFont(font);
        createTime.setFont(font);
        createTimeField.setFont(font);
        PType.setFont(font);
        PTypeField.setFont(font);
        price.setFont(font);
        priceField.setFont(font);
        pro.setFont(font);
        proField.setFont(font);
        btn2.setFont(font);
        btn3.setFont(font);

        frame.add(name);
        frame.add(NameField);
        frame.add(pro);
        frame.add(proField);
        frame.add(consumeTime);
        frame.add(consumeTimeField);
        frame.add(pId);
        frame.add(pIdField);
        frame.add(createTime);
        frame.add(createTimeField);
        frame.add(PType);
        frame.add(PTypeField);
        frame.add(price);
        frame.add(priceField);

        label.setBounds(150, 20, 360, 30);
        name.setBounds(40, 100, 140, 40);
        NameField.setBounds(200, 100, 300, 40);
        pro.setBounds(40, 150, 100, 40);
        proField.setBounds(200, 150, 300, 40);
        consumeTime.setBounds(40, 200, 100, 40);
        consumeTimeField.setBounds(200, 200, 300, 40);
        pId.setBounds(40, 250, 100, 40);
        pIdField.setBounds(200, 250, 300, 40);
        createTime.setBounds(40, 300, 140, 40);
        createTimeField.setBounds(200, 300, 300, 40);
        PType.setBounds(40, 350, 140, 40);
        PTypeField.setBounds(200, 350, 300, 40);
        price.setBounds(40, 400, 140, 40);
        priceField.setBounds(200, 400, 300, 40);
        btn2.setBounds(320, 500, 100, 35);
        btn3.setBounds(120, 500, 140, 35);

        btn2.addActionListener(this);
        btn3.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        CustomerSqlSeverImpl dao = new CustomerSqlSeverImpl();
        Customer customer = new Customer();

        if ("修改".equals(e.getActionCommand())) {
            customer.setCustomer_name(NameField.getText());
            customer.setPrice(Integer.valueOf(priceField.getText()));
            customer.setProducts(proField.getText());
            customer.setProduct_id(pIdField.getText());
            customer.setCreate_time(createTimeField.getText());
            customer.setProduct_type(PTypeField.getText());
//            customer.setConsume_time(consumeTimeField.getText());

            boolean i = dao.updatePro(customer);
            System.out.println(i);
            if (i) {
                JOptionPane.showMessageDialog(null, "修改成功！");
            }

        } else if ("清空".equals(e.getActionCommand())) {
            NameField.setText("");
            consumeTimeField.setText("");
            pIdField.setText("");
            createTimeField.setText("");
            PTypeField.setText("");
            priceField.setText("");
            proField.setText("");

        }
    }
}
